package cn.elves.model.strategy.service.impl;

import cn.elves.model.strategy.entity.dto.StrategyDTO;
import cn.elves.model.strategy.entity.dto.StrategyDetailDTO;
import cn.elves.model.strategy.entity.po.Strategy;
import cn.elves.model.strategy.entity.po.StrategyDetail;
import cn.elves.model.strategy.mapper.StrategyMapper;
import cn.elves.model.strategy.service.StrategyDetailService;
import cn.elves.model.strategy.service.StrategyService;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author pc
* @description 针对表【strategy】的数据库操作Service实现
* @createDate 2024-06-16 12:48:17
*/
@Service
public class StrategyServiceImpl extends ServiceImpl<StrategyMapper, Strategy>
    implements StrategyService {

    @Resource
    private StrategyDetailService strategyDetailService;

    @Resource
    private StrategyMapper strategyMapper;


    @Override
    public void updateStrategy(StrategyDTO strategyDTO) {
        Strategy strategy = new Strategy();
        BeanUtils.copyProperties(strategyDTO,strategy);
        strategyMapper.updateById(strategy);
        List<StrategyDetailDTO> strategyDetailDTOList = strategyDTO.getStrategyDetailDTOList();
        if(CollectionUtils.isEmpty(strategyDetailDTOList)){
            return;
        }
        List<StrategyDetail> strategyDetailList = strategyDetailDTOList.stream().map(item -> {
            StrategyDetail strategyDetail = new StrategyDetail();
            BeanUtils.copyProperties(item, strategyDetail);
            return strategyDetail;
        }).collect(Collectors.toList());

        strategyDetailService.saveOrUpdateBatch(strategyDetailList);
    }
}




